define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'gameuser/index',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'uid',
                sortName: 'uid',
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                     	{field: 'head', title: __('Head'),formatter:Table.api.formatter.image, operate: false},
                        {field: 'uid', title: __('Uid'), placeholder: '最多20个玩家ID，ID以逗号分隔'},
                        {field: 'nickname', title: __('Nickname'), operate: false},
                        {field: 'in_club', title: __('状态'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        	formatter: function (value, row, index) {
            				if(row.in_club == '未加入'){
            					return '<a title="邀请加入" class="btn btn-xs btn-info btn-add" href="/club/gameuser/add/clubid/'+row.clubid+'/uid/'+row.uid+'"> 邀请加入</a>';
            				}
						}}
                    ]
                ],
                onLoadSuccess: function (data) {
                	if(data.code == 0){
                		Layer.alert(data.msg);
                	}
	            }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            //点击包含.btn-ajax的元素时发送Ajax请求
            $(document).on('click', '.btn-add', function (e) {
                var options = $(this).data();
                if (typeof options.url === 'undefined' && $(this).attr("href")) {
                    options.url = $(this).attr("href");
                }
            	layer.confirm('是否邀请他（她）加入俱乐部？', {
        			btn: ['确定','取消']
        		}, function(){
                    $.post(options.url).success(function(data){
                        if (data.code==1) {
                            if (data.msg) {
                            	Layer.alert(data.msg);
                            }
                            table.bootstrapTable('refresh');
                        }else{
                        	Layer.alert(data.msg);
                        }
                    });
        		}, function(){
//        			table.bootstrapTable('refresh');
        		});
                return false;
            });
            
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});